#ifndef BOOCAXSOCKET_H
#define BOOCAXSOCKET_H

#include <QTcpSocket>
#include <QHostAddress>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

#include <QtDebug>

class BoocaxSocket : public QTcpSocket
{
    Q_OBJECT
    Q_PROPERTY(double poseX READ poseX WRITE setPoseX NOTIFY poseXChanged)
    Q_PROPERTY(double poseY READ poseY WRITE setPoseY NOTIFY poseYChanged)
    Q_PROPERTY(double poseYaw READ poseYaw WRITE setPoseYaw NOTIFY poseYawChanged)
    Q_PROPERTY(double velVx READ velVx WRITE setVelVx NOTIFY velVxChanged)
    Q_PROPERTY(double velVy READ velVy WRITE setVelVy NOTIFY velVyChanged)
    Q_PROPERTY(double velVtheta READ velVtheta WRITE setVelVtheta NOTIFY velVthetaChanged)
    Q_PROPERTY(bool uwb READ uwb WRITE setUwb NOTIFY uwbChanged)
    Q_PROPERTY(bool laser READ laser WRITE setLaser NOTIFY laserChanged)
    Q_PROPERTY(bool base READ base WRITE setBase NOTIFY baseChanged)
    Q_PROPERTY(bool odom READ odom WRITE setOdom NOTIFY odomChanged)
    Q_PROPERTY(int locStatus READ locStatus WRITE setLocStatus NOTIFY locStatusChanged)
    Q_PROPERTY(int moveStatus READ moveStatus WRITE setMoveStatus NOTIFY moveStatusChanged)
    Q_PROPERTY(double timeStatus READ timeStatus WRITE setTimeStatus NOTIFY timeStatusChanged)
    Q_PROPERTY(double dist READ dist WRITE setDist NOTIFY distChanged)
    Q_PROPERTY(int navCount READ navCount WRITE setNavCount NOTIFY navCountChanged)

public:
    explicit BoocaxSocket(QTcpSocket *parent = nullptr);
    ~BoocaxSocket();

    bool connectToBoocax();
    void writeJsonData(QJsonObject &obj);

public slots:
    void readJsonData();

private:
    void parserJsonData(QJsonObject &jsonObj);

private:
    QHostAddress *hostAddress;
    int port;

    //read json data
    int readByteCount;

signals:
    void basicStatusChanged();
    void reportPosVelStatusChanged();

    void poseXChanged();
    void poseYChanged();
    void poseYawChanged();
    void velVxChanged();
    void velVyChanged();
    void velVthetaChanged();
    void uwbChanged();
    void laserChanged();
    void baseChanged();
    void odomChanged();
    void locStatusChanged();
    void moveStatusChanged();
    void timeStatusChanged();
    void distChanged();
    void navCountChanged();

public:
    //当前位置，横坐标
    double poseX() { return _poseX; }
    void setPoseX(double poseX_)
    {
        if(_poseX != poseX_)
        {
            _poseX = poseX_;
            emit poseXChanged();
        }
    }
    //当前位置，纵坐标
    double poseY() { return _poseY; }
    void setPoseY(double poseY_)
    {
        if(_poseY != poseY_)
        {
            _poseY = poseY_;
            emit poseYChanged();
        }
    }
    //当前位置，方向
    double poseYaw() { return _poseYaw; }
    void setPoseYaw(double poseYaw_)
    {
        if(_poseYaw != poseYaw_)
        {
            _poseYaw = poseYaw_;
            emit poseYawChanged();
        }
    }
    //当前位置，x方向速度
    double velVx() { return _velVx; }
    void setVelVx(double velVx_)
    {
        if(_velVx != velVx_)
        {
            _velVx = velVx_;
            emit velVxChanged();
        }
    }
    //当前位置，y方向速度
    double velVy() { return _velVy; }
    void setVelVy(double velVy_)
    {
        if(_velVy != velVy_)
        {
            _velVy = velVy_;
            emit velVyChanged();
        }
    }
    //当前位置，角速度
    double velVtheta() { return _velVtheta; }
    void setVelVtheta(double velVtheta_)
    {
        if(_velVtheta != velVtheta_)
        {
            _velVtheta = velVtheta_;
            emit velVthetaChanged();
        }
    }

    //UWB标签状态
    bool uwb() { return _uwb; }
    void setUwb(bool uwb_)
    {
        if(_uwb != uwb_)
        {
            _uwb = uwb_;
            emit uwbChanged();
        }
    }
    //激光雷达状态
    bool laser() { return _laser; }
    void setLaser(bool laser_)
    {
        if(_laser != laser_)
        {
            _laser = laser_;
            emit laserChanged();
        }
    }
    //底盘状态
    bool base() { return _base; }
    void setBase(bool base_)
    {
        if(_base != base_)
        {
            _base = base_;
            emit baseChanged();
        }
    }
    //里程计状态
    bool odom() { return _odom; }
    void setOdom(bool odom_)
    {
        if(_odom != odom_)
        {
            _odom = odom_;
            emit odomChanged();
        }
    }

    //机器人定位状态
    int locStatus() { return _locStatus; }
    void setLocStatus(int locStatus_)
    {
        if(_locStatus != locStatus_)
        {
            _locStatus = locStatus_;
            emit locStatusChanged();
        }
    }
    //机器人导航移动状态
    int moveStatus() { return _moveStatus; }
    void setMoveStatus(int moveStatus_)
    {
        if(_moveStatus != moveStatus_)
        {
            _moveStatus = moveStatus_;
            emit moveStatusChanged();
        }
    }
    //导航移动总时间
    double timeStatus() { return _timeStatus; }
    void setTimeStatus(double timeStatus_)
    {
        if(_timeStatus != timeStatus_)
        {
            _timeStatus = timeStatus_;
            emit timeStatusChanged();
        }
    }
    //导航里程
    double dist() { return _dist; }
    void setDist(double dist_)
    {
        if(_dist != dist_)
        {
            _dist = dist_;
            emit distChanged();
        }
    }
    //成功导航次数
    int navCount() { return _navCount; }
    void setNavCount(int navCount_)
    {
        if(_navCount != navCount_)
        {
            _navCount = navCount_;
            emit navCountChanged();
        }
    }

private:
    double _poseX;
    double _poseY;
    double _poseYaw;
    double _velVx;
    double _velVy;
    double _velVtheta;
    bool _uwb;
    bool _laser;
    bool _base;
    bool _odom;
    int _locStatus;
    int _moveStatus;
    double _timeStatus;
    double _dist;
    int _navCount;
};

#endif // BOOCAXSOCKET_H
